package com.lawyer.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.lawyer.entity.Coupon;
import com.lawyer.entity.MealConfiguration;
import com.lawyer.model.Message;
import com.lawyer.model.MessageConstant;
import com.lawyer.repository.ConfigRepository;
import com.lawyer.repository.CouponRepository;
import com.lawyer.repository.MealConfigurationRepository;
import com.lawyer.repository.UserRepository;
import com.lawyer.uitls.DateUtils;

@RestController
@RequestMapping(value = "/meal")
@Api(tags = "套餐相关")
@SuppressWarnings("all")
public class MealConfigController {

	protected final Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private UserRepository userRepository;
	@Autowired
	private CouponRepository couponRepository;
	@Autowired
	private ConfigRepository configRepository;
	@Autowired
	private MealConfigurationRepository mealConfigurationRepository;


	@ApiOperation(httpMethod = "GET", value = "获取套餐列表", notes = "餐套类型,1=余额充值套餐，2=电话咨询套餐,3=提现金额例表")
	@RequestMapping(value = "/queryByMealType", method = RequestMethod.GET)
	public HttpEntity<?> queryByMealType(Integer mealType, String userId) {
		Message msg = new Message();
		try {
		
			List<MealConfiguration> list = new ArrayList<MealConfiguration>();
			List<Coupon> coupons = couponRepository.findByUserIdAndIsUsedAndIsExpire(userId, false,false);
			
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			
			Iterator<Coupon> iterator = coupons.iterator();
			while (iterator.hasNext()) {
				Coupon coupon = iterator.next();
				String endDate = sdf.format(coupon.getEndDate());
				if (DateUtils.checktime(endDate)) {
					// 已过期
					couponRepository.delete(coupon);
					iterator.remove();
				}
			}
			
			
			if (coupons != null && coupons.size() > 0) {
				MealConfiguration meal = new MealConfiguration();
				meal.setId(0);
				meal.setMealType(2);
				meal.setPrice(0D);
				meal.setReducedPrice(0D);
				meal.setTime(coupons.get(0).getTime());
				meal.setCouponId(coupons.get(0).getId());
				meal.setDescription(coupons.get(0).getDescription());
				list.add(meal);
			}
			
			
			List<MealConfiguration> listMeal = mealConfigurationRepository.findByMealType(mealType);
			list.addAll(listMeal);
			
			msg.setData(list);
		} catch (Exception e) {
			e.printStackTrace();
			msg.setCode(MessageConstant.FAILED_CODE);
			msg.setMessage(MessageConstant.FAILED_MSG);
		}

		return new ResponseEntity<>(msg, HttpStatus.OK);
	}

}
